//
//  THud.m
//  shenmajinrong
//
//  Created by 罗西 on 10/27/15.
//  Copyright © 2015 com.demo. All rights reserved.
//

#import "THud.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"

@interface THud (){
    dispatch_semaphore_t sem;
}

@property (nonatomic, strong) MBProgressHUD *hud;

@end

@implementation THud



+(THud *)sharedInstance{
    static THud *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[THud alloc] init];
    });
    return instance;
}

-(id)init{
    self = [super init];
    if (self) {
        _hud = [[MBProgressHUD alloc] initWithWindow:[AppDelegate sharedDelegate].window ];
        _hud.labelFont = [UIFont systemFontOfSize:15];
        _hud.mode = MBProgressHUDModeIndeterminate;
    }
    return self;
}

- (void)showTips:(NSString *)message{
    _hud.labelText = message;
    [_hud show:YES];
    [[AppDelegate sharedDelegate].window addSubview:_hud];
    //[_hud hide:YES afterDelay:2.0];
}

- (void)showText:(NSString *)message{
    _hud.labelText = message;
    _hud.mode = MBProgressHUDModeText;
    [_hud show:YES];
    [[AppDelegate sharedDelegate].window addSubview:_hud];
    _hud.removeFromSuperViewOnHide = YES;
    [_hud hide:YES afterDelay:1.0];
}

-(void)showText:(NSString *)message completionBlock:(void (^)())completionBlock{
    _hud.labelText = message;
    _hud.mode = MBProgressHUDModeText;
    //[_hud show\]
    [_hud showAnimated:YES whileExecutingBlock:nil completionBlock:^{
        completionBlock();
        _hud.removeFromSuperViewOnHide = YES;
        [_hud hide:YES afterDelay:1.0];
    }];
    [[AppDelegate sharedDelegate].window addSubview:_hud];
}

- (void)showText:(NSString *)message Delay:(float)count{
    _hud.labelText = message;
    _hud.mode = MBProgressHUDModeText;
    [_hud show:YES];
    [[AppDelegate sharedDelegate].window addSubview:_hud];
    _hud.removeFromSuperViewOnHide = YES;
    [_hud hide:YES afterDelay:count];
}

- (void)showLongText:(NSArray *)strArray{
    NSMutableString *str = [NSMutableString new];
    for(id obj in strArray){
        [str appendString:[NSString stringWithFormat:@"-%@\n",obj]];
    }
    
    _hud.mode = MBProgressHUDModeCustomView;
    UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    tv.text = str;
    _hud.customView = tv;
    [_hud show:YES];
    [[AppDelegate sharedDelegate].window addSubview:_hud];
    _hud.removeFromSuperViewOnHide = YES;
    [_hud hide:YES afterDelay:2.0];
}

-(void)hideHud{
    _hud.removeFromSuperViewOnHide = YES;
    [_hud hide:YES];
    //[_hud removeFromSuperview];
}

- (void)showProgressDialog:(NSString *)message {
    _hud.labelText = message;
    _hud.mode = MBProgressHUDModeDeterminate;
    [_hud show:YES];
    [[AppDelegate sharedDelegate].window addSubview:_hud];
    _hud.removeFromSuperViewOnHide = YES;
}

- (void)setProgressDialog:(float)progress{
    _hud.progress = progress;
}

-(void)showProgress{
     _hud.mode = MBProgressHUDModeIndeterminate;
    _hud.labelText = nil;
    [_hud show:YES];
    [[AppDelegate sharedDelegate].window addSubview:_hud];
    _hud.removeFromSuperViewOnHide = YES;
}

@end
